package com.community.web.controller.system;

import java.util.List;
import java.util.Set;

import com.community.common.utils.StringUtils;
import com.community.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.community.common.constant.Constants;
import com.community.common.core.domain.AjaxResult;
import com.community.common.core.domain.entity.SysMenu;
import com.community.common.core.domain.entity.SysUser;
import com.community.common.core.domain.model.LoginBody;
import com.community.common.utils.SecurityUtils;
import com.community.framework.web.service.SysLoginService;
import com.community.framework.web.service.SysPermissionService;
import com.community.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author community
 */
@RestController
@Api("登录相关接口")
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService iSysUserService;

    @Value("${manager.phone}")
    private String managerPhone;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录接口")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * description 校验用户是否存在
     * @author chenwy
     * @param username
     * @return AjaxResult
     */
    @ApiOperation("校验手机号是否注册")
    @GetMapping("/checkRegister/{username}")
    public AjaxResult checkRegister(@PathVariable("username") String username){
        SysUser user = iSysUserService.selectUserByUserName(username);
        if(user != null){
            return AjaxResult.success(true);
        }
        return AjaxResult.success(false);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否管理员，是否有权限审批
        if(managerPhone.contains(user.getUserName())){
            user.setIsManager(1);
        }else {
            user.setIsManager(0);
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
